perm filename GLOBAL.PAL[HAL,HE] blob sn#182250 filedate 1975-10-20 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00002 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	Global reference routines GLOBGT, GLOBSR, GGTVAL, GCHANGE.
C00007 ENDMK
C⊗;
;Global reference routines GLOBGT, GLOBSR, GGTVAL, GCHANGE.

GLBLNK:	;Interpreter routine
COMMENT ⊗ Expects two arguments at the IPC, a level-offset, and two
words of a Rad50 name.  Makes sure that this global is linked in to
the environment at the given level-offset.  If not, a search is
made for it, and the result is put in the environment.
⊗
	MOV @IPC(R4),R0	;R0 ← level-offset
	BMPIPC		;Bump IPC past the level-offset
	JSR PC,GETARG	;R0 ← LOC[environment cell]
	TST (R0)	;Graph node yet?
	BEQ GLOBG1	;No, must search for it
GLOBG2:	BMPIPC		;Bump IPC past the Rad50 name
	BMPIPC		;Bump IPC past the Rad50 name
	RTS PC		;Done
GLOBG1:	MOV R0,R2	;R2 ← LOC[environment cell]
	MOV IPC(R4),R0	;R0 ← LOC[Rad50 representation]
	JSR PC,GLOBSR	;R0 ← LOC[new or old graph node]
	MOV R0,@(SP)+	;Stow LOC[graph node] in the environment cell
	BR GLOBG2	;Ready to return

MAXGLB == 10		;Maximum number of globals allowed
GLBTAB:	.WORD 3*MAXGLB	;Three words per global:  2 of Rad50, one
				;pointer to the graph node.
				;To be searched linearly.
GLBLIM:	.WORD 3		;Overflow place for GLBTAB
GLBEND:	.WORD 1		;Points to next free place in GLBTAB

GLOBSR:
COMMENT ⊗ R0 = LOC[two words of Rad50].  Tries to find the
appropriate graph node using the GLBTAB, and if it fails, makes a new
graph node and inserts it in the GLBTAB.  In any case, returns R0 ←
LOC[new or old graph node].  ⊗
	EVWAIT GLBEVT	;Critical region starts here
	MOV GLBEND,R1	;R1 ← LOC[next free place in GLBTAB]
	MOV (R0),(R1)+	;Put the word sought at next free place
	MOV 2(R0),(R1)+	;
	CLR (R1)	; with a 0 for a graph node pointer.
	MOV #GLBTAB,R1	;R1 ← LOC[start of GLBTAB]
GLOBS3:	CMP (R0),(R1)	;MATCH?
	BNE GLOBS1	;No.
	CMP 2(R0),2(R1)	;Second word match?
	BEQ GLOBS2	;Yes.
GLOBS1:	ADD #6,R1	;
	BR  GLOBS3	;Try again.
GLOBS2:	MOV 4(R1),R0	;R0 ← LOC[graph node]
	BNE GLOBS6	;If it is not zero, we are done
	ADD #6,GLBEND	;Move the end of the table down one entry
	CMP GLBEND,#GLBLIM	;Too far?
	BLT GLOBS5	;No
	HALERR GLOBMS	;Yes
GLOBS5:	MOV R1,-(SP)	;Save place in GLBTAB
	CLR R0		;New graph node should have no value cell.
	JSR PC,MAKEGN	;R0 ← LOC[a new graph node]
	MOV (SP)+,R1	;Restore place in GLBTAB
	MOV R0,4(R1)	;store LOC[new graph node] in GLBTAB
GLOBS6:	EVSIG GLBEVT	;Critical region ends here
	RTS PC		;Done